package com.crm.jpaandthymeleaf.web.controller;

import com.crm.jpaandthymeleaf.entity.SysRole;
import com.crm.jpaandthymeleaf.entity.SysUser;
import com.crm.jpaandthymeleaf.service.ISysRoleService;
import com.crm.jpaandthymeleaf.service.ISysUserService;
import com.crm.jpaandthymeleaf.service.PasswordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周敬
 * @version 1.0
 * @packageName com.crm.jpaandthymeleaf.web.controller
 * @fileName SysUserController
 * @createTime 2021/12/16-9:31-星期四
 * @lastModify 2021/12/16-9:31-星期四
 */
@Controller
@RequestMapping("/user")
public class SysUserController {
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private ISysRoleService sysRoleService;
    @Resource
    private PasswordService passwordService;

    @RequestMapping("/list")
    public String list(Model model, String usrName,
                       @RequestParam(required = false, defaultValue = "0") Long roleId,
                       @RequestParam(value = "pageNo", required = false, defaultValue = "1") int pageIndex) {
        Pageable pageable = PageRequest.of(pageIndex - 1, 5, Sort.by(Sort.Direction.ASC, "usrId"));
        Page<SysUser> userPager = sysUserService.findSysUsers(usrName, roleId, pageable);
        model.addAttribute("usrName", usrName);
        model.addAttribute("userPager", userPager);
        model.addAttribute("roleId", roleId);
        List<SysRole> roles = sysRoleService.findAllSysRole();
        model.addAttribute("roles", roles);
        //总数据量
        long total = userPager.getTotalElements();
        //总页数
        int pageCount = userPager.getTotalPages();
        //当前一页显示数量
        int size = userPager.getSize();
        //当前页
        int number = userPager.getNumber();
        return "user/list";
    }

    @GetMapping("/add")
    public String add(Model model) {
        List<SysRole> roles = sysRoleService.findAllSysRole();
        model.addAttribute("roles", roles);
        return "/user/add";
    }

    @PostMapping("/save")
    public String save(SysUser user){
        String encryptPassword = passwordService.encryptPassword(user.getUsrName(), user.getUsrPassword());
        user.setUsrFlag(1L);
        user.setUsrPassword(encryptPassword);
        sysUserService.saveSysUser(user);
        return "redirect:/user/list";
    }

    @GetMapping("/edit/{usrId}")
    @RequiresPermissions(value = "用户编辑")
    public String toEdit(@PathVariable Long usrId, Model model){
        SysUser sysUser = sysUserService.getSysUser(usrId);
        List<SysRole> roles = sysRoleService.findAllSysRole();
        model.addAttribute("sysUser",sysUser);
        model.addAttribute("roles",roles);
        return "/user/edit";
    }

    @PostMapping("/edit")
    public String edit(SysUser user){
        sysUserService.saveSysUser(user);
        return "redirect:/user/list";
    }

    @GetMapping("/del/{usrId}")
    @ResponseBody
    public Map del(@PathVariable Long usrId){
        sysUserService.deleteSysUser(usrId);
        Map map = new HashMap();
        map.put("delResult","true");
        return map;
    }
}
